package com.ww.core.widget.dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.ww.core.R;

public class ListCheckDialog {
	Context context;
	Dialog dlg;

	ListView listview;
	TextView title;
	Button cancel, ok;
	CheckAdapter radioAdapter;
	List<Integer> listItemID = new ArrayList<Integer>();

	public ListCheckDialog(Context context) {
		this.context = context;
		dlg = new Dialog(context, R.style.MyDialog);
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_check);
		listview = (ListView) window.findViewById(R.id.listView);
		ok = (Button) window.findViewById(R.id.ok);
		cancel = (Button) window.findViewById(R.id.cancel);
		title = (TextView) window.findViewById(R.id.title);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cancel();
			}
		});
	}

	public void show(String[] items, boolean[] bools, OnClickListener clickListener) {
		radioAdapter = new CheckAdapter(context, items, bools);
		listview.setAdapter(radioAdapter);
		dlg.show();
		ok.setOnClickListener(clickListener);
	}

	public List<Integer> getResult() {
		listItemID.clear();
		for (int i = 0; i < radioAdapter.mChecked.size(); i++) {
			if (radioAdapter.mChecked.get(i)) {
				listItemID.add(i);
			}
		}
		return listItemID;
	}

	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}

	class CheckAdapter extends BaseAdapter {
		List<Boolean> mChecked;
		HashMap<Integer, View> map = new HashMap<Integer, View>();
		private Context context;
		private String[] list;

		public CheckAdapter(Context context, String[] list, boolean[] bools) {
			this.context = context;
			this.list = list;

			mChecked = new ArrayList<Boolean>();
			for (int i = 0; i < list.length; i++) {
				mChecked.add(bools[i]);
			}
		}

		public int getCount() {
			return list.length;
		}

		public Object getItem(int position) {
			 return list[position];  
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			View view;
			ViewHolder holder = null;

			if (map.get(position) == null) {

				LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = mInflater.inflate(R.layout.core_dialog_check_item, null);
				holder = new ViewHolder();
				holder.check = (CheckBox) view.findViewById(R.id.check);
				holder.txt = (TextView) view.findViewById(R.id.txt);
				final int p = position;
				map.put(position, view);
				holder.check.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						CheckBox cb = (CheckBox) v;
						mChecked.set(p, cb.isChecked());
					}
				});
				view.setTag(holder);
			} else {
				view = map.get(position);
				holder = (ViewHolder) view.getTag();
			}

			holder.check.setChecked(mChecked.get(position));
			holder.txt.setText(list[position]);

			return view;
		}
	}

	/**
	 * @version 2013-3-21
	 * @author wangw
	 */
	private static class ViewHolder {
		private CheckBox check;
		private TextView txt;

	}
}